//
//  userAcoountModel.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//

import UIKit

import AFNetworking


//ViewModel 将一些业务逻辑封装到 这个类里  让这个隔离的更加好
class userAccountViewModel: NSObject {
    
    //增加userAccount 属性
    var account: userAccount?
    
    
    //用户是否登录的属性
    var userLogin: Bool {
        return account?.access_token != nil
    }
    
    var token: String? {
        return account?.access_token
    }
    
    //昵称
    var userName : String? {
    
    return account?.name
    }
    
    //用户头像
    
    var hearUrl : NSURL? {
    
    return NSURL(string: (account?.avatar_large ?? ""))
    
    }
    
    //在init方法中获取 用户账户
    override init() {
        account = userAccount.loadAccount()
    }
    
    ////加载用户token
    func loadAccessToken(code: String, finished: (error: NSError?) -> ()) {
        //修改,进行前缀
        let urlString = "oauth2/access_token"
        let parameters = ["client_id": client_id,"client_secret":client_secret,"grant_type" : "authorization_code","code":code,"redirect_uri":redirect_uri]
//        let AFN = AFHTTPSessionManager()
//        //failed: unacceptable content-type: text/html  text/plain
//        //AFN 默认只支持以下几种数据格式的解析 @"application/json", @"text/json", @"text/javascript"
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
//        AFN.POST(urlString, parameters: parameters, success: { (_, result) -> Void in
//            if let dict = result as? [String: AnyObject] {
        
        NetworkTools.shardTools.requestJsonDict(.POST, urlString: urlString, paraments: parameters) { (result, error) -> () in
            if error != nil {
                print("=--=-=-=-=-=-=-=")
                print(error)
            //错误回调
            finished(error: error)
            return
            
        }
                //开始字典转模型
                let account = userAccount(dict:result!)
                self.loadUserInfo(account, finished: finished)
            }
            
//            }) { (_, error) -> Void in
//                print(error)
//        }
    }
    
    
    //加载用户信息
    private func loadUserInfo(account: userAccount,finished: (error: NSError?) -> ()) {
        //修改,进行前缀
        let urlString = "2/users/show.json"
        let parmaters = ["access_token": account.access_token!, "uid": account.uid!]
//        let AFN = AFHTTPSessionManager()
//        AFN.GET(urlString, parameters: parmaters, success: { (_, result) -> Void in
        
        NetworkTools.shardTools.requestJsonDict(.GET, urlString: urlString, paraments: parmaters) { (result, error) -> () in
            
            if error != nil {
            
            finished(error: error)
                
            }
            

//            if let dict = result as? [String : AnyObject] {
                account.avatar_large = result!["avatar_large"] as? String
                account.name = result!["name"] as? String
                
                account.saveAccount()
                
                ///执行闭包
                finished(error: nil)
//            }
//            }) { (_, error) -> Void in
//                finished(error: error)
//                print(error)
//        }
    }
  }
}